#include<string.h>
#include<stdio.h>
#define MAX_IDNUM 20
#define MAX_NAME 20
#define MAX 100
typedef struct Student
{
    char ID[MAX_IDNUM];
    char name[MAX_NAME];
    int grade;
}Student;

typedef struct Table
{
    Student data[MAX];
    int sn;
}Table;

void InitStudent(Table*t);

void minu()
{
    printf("**************************************\n");
    printf("**************************************\n");
    printf("* 1.add        2.print        3.exit *\n");
    printf("**************************************\n");
    printf("**************************************\n");
}

void AddStudent(Table*t);

void PrintStudent(Table*t);

void LoadFile(Table*t);

void SaveFile(Table*t);

int main()
{
    Table tab;
    InitStudent(&tab);
    LoadFile(&tab);

    do
    {
        int n;
       minu();
       printf("Please entry:\n");
       scanf("%d",&n);
       switch(n)
       {
           case 1:
           AddStudent(&tab);
           SaveFile(&tab);
           break;

           case 2:
           PrintStudent(&tab);
           break;

           case 3:
           exit(1);
           break;
           default:
           printf("Your entry a wrong number,PLEASE ENTRY IT AGAIN!\n");
           break;

       }


    } while (1);
    
    return 0;

}

void InitStudent(Table*t)
{
    t->sn=0;
    memset(t->data,0,sizeof(t->data));
    return;
}

void AddStudent(Table*t)
{
    if(t->sn==MAX)
    {
        printf("The information has already full.\n");
    }
    else{
        printf("Please entry the student'ID number:\n");
        scanf("%s",t->data[t->sn].ID);
        printf("Please entry the student's name:\n");
        scanf("%s",t->data[t->sn].name);
        printf("Please entry the student's grade:\n");
        scanf("%d",&(t->data[t->sn].grade));
        t->sn++;
    }
    return;
}

void PrintStudent(Table*t)
{
    printf("%-15s\t%-15s\t %-5s\n","ID number","name","grade");
    for(int i=0;i<t->sn;i++)
    {
        printf("%-15s\t%-15s\t %-5d\n",t->data[i].ID,t->data[i].name,t->data[i].grade);
    }
    printf("\n");
    return;
}

void LoadFile(Table*t)
{
    FILE*fp;
    fp=fopen("student data.txt","rb");
    if (fp==NULL)
    {
        char c;
        printf("The file \"student data.txt\" doesn't exist,do you want to build one?(y/n)\n");
        scanf("%c",&c);
        if(c=='y')
        {
            return;
        }
        else
        {
            exit(1);
        }

    }
    else
    {
       
        Student tep={0};
        while(fread(&tep,sizeof(Student),1,fp))
        {
            t->data[t->sn]=tep;
            t->sn++;
        }

        fclose(fp);
        fp=NULL;
        return 0;
    }

}

void SaveFile(Table*t)
{
    FILE*fw;
    fw=fopen("student data.txt","wb");
    for(int i=0;i<t->sn;i++)
    {
        //fprintf(fw,"%s %s %d",t->data[i].ID,t->data[i].name,t->data[i].grade);
        fwrite(t->data+i,sizeof(t->data[i]),1,fw);
    }
    fclose(fw);
    fw=NULL;
    return;

}